package algorithm;

import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ZhiShuFenJieDemo {

  private static Random random = new Random();
  private int illegalArgumentCount = 0;

  public static void main(String[] args) {
    ZhiShuFenJieDemo zhiShuFenJieDemo = new ZhiShuFenJieDemo();
    List<Integer> integers = zhiShuFenJieDemo.primeFactors(9);
    for (Integer integer : integers) {
      System.out.println(integer);
    }
  }

  public List<Integer> primeFactors(int number) {
    if (number < 2) {
      ++this.illegalArgumentCount;
      throw new IllegalArgumentException("number is: " + number + ", need >= 2");
    }
    ArrayList<Integer> result = new ArrayList<Integer>();
    int i = 2;
    while (i <= number) {
      if (number % i == 0) {
        result.add(i);
        number /= i;
        i = 2;
        continue;
      }
      ++i;
    }
    return result;
  }

  @Test
  public void test() {
    // 2 27
    // 2 3 9
    // 2 3 3 3
    int number = 54 * 2;

    List<Integer> list = Lists.newArrayList();

    int i = 2;
    while (i <= number) {
      if (number % i == 0) {
        number /= i;
        list.add(i);
        i = 2;
      } else {
        i++;
      }
    }
    for (Integer integer : list) {
      System.out.print(integer + "\t");
    }
  }

}
